package com.zxt.content.copywriting.events;

import com.alibaba.fastjson.JSON;
import com.zxt.content.copywriting.events.upload.UploadCopywritingEvent;
import com.zxt.content.copywriting.events.upload.UploadCopywritingEventContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

/**
 * 文案事件发布器，通知相关业务组件处理对应数据
 * 使用事件机制是为了个业务逻辑解耦
 * 发布
 *
 * @author zxt
 */
@Component
@Slf4j
public class CopywritingEventPublisher implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public String uploadCopywriting(UploadCopywritingEventContext context) {
        log.info("上传私有文案到公共文案 入参:{}", JSON.toJSONString(context));
        applicationEventPublisher.publishEvent(new UploadCopywritingEvent(this, context));
        log.info("上传私有文案到公共文案 出参:{}", context.getResult());
        return context.getResult();
    }
}
